package com.yserp.kpmd.controller;
import com.yserp.kp.entity.TKpOrderCpEntity;
import com.yserp.kplog.entity.TKpJiageLogEntity;
import com.yserp.kplog.service.TKpJiageLogServiceI;
import com.yserp.kpmd.entity.*;
import com.yserp.kpmd.service.TKpJiageServiceI;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yserp.mm.entity.TMmPoGrHeadEntity;
import com.yserp.mm.entity.TMmPoGrItemEntity;
import com.yserp.open.entity.VMmOpenPoEntity;
import com.yserp.open.page.PoPage;
import com.yserp.open.page.SdorderPage;
import com.yserp.util.erpUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.TreeChildCount;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import java.io.OutputStream;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecgframework.core.util.ResourceUtil;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.core.util.ExceptionUtil;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.jwt.util.GsonUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**   
 * @Title: Controller  
 * @Description: 销售价格
 * @author onlineGenerator
 * @date 2018-05-23 16:29:34
 * @version V1.0   
 *
 */
@Api(value="TKpJiage",description="销售价格",tags="tKpJiageController")
@Controller
@RequestMapping("/tKpJiageController")
public class TKpJiageController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TKpJiageController.class);

	@Autowired
	private TKpJiageServiceI tKpJiageService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;


	@Autowired
	private TKpJiageLogServiceI tKpJiageLogService;
	/**
	 * 销售价格列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kpmd/tKpJiageList");
	}
	@RequestMapping(params = "listview")
	public ModelAndView listview(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kpmd/tKpJiageviewList");
	}
	@RequestMapping(params = "listkp")
	public ModelAndView listkp(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kp/kpList");
	}
	@RequestMapping(params = "listxhkp")
	public ModelAndView listxhkp(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kp/xhkpList");
	}
	@RequestMapping(params = "listsp")
	public ModelAndView listsp(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kpmd/tKpJiagespList");
	}

	@RequestMapping(params = "listsh")
	public ModelAndView listsh(HttpServletRequest request) {
		return new ModelAndView("com/yserp/kpmd/tKpJiageshList");
	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(String bpmStatus,TKpJiageEntity tKpJiage,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(TKpJiageEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tKpJiage, request.getParameterMap());
		try{
		//自定义追加查询条件
			if (!StringUtil.isEmpty(bpmStatus)){
				cq.eq("bpmStatus",bpmStatus);
			}
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.tKpJiageService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	/**
	 * 保存新增/更新的行数据
	 * @param page
	 * @return
	 */
	@RequestMapping(params = "saveRows")
	@ResponseBody
	public AjaxJson saveRows(jiagePage page){

		String message = null;
		List<TKpJiageEntity> demos=page.getDemos();
		AjaxJson j = new AjaxJson();
		if(CollectionUtils.isNotEmpty(demos)){
			for(TKpJiageEntity jeecgDemo:demos){
				if (StringUtil.isNotEmpty(jeecgDemo.getId())) {
					TKpJiageEntity t =tKpJiageService.get(TKpJiageEntity.class, jeecgDemo.getId());

					try {
                        t.setCpDshenjia(jeecgDemo.getCpDshenjia());
						t.setChuchangfei(jeecgDemo.getChuchangfei());
						t.setYunshufei(jeecgDemo.getYunshufei());

						t.setBpmStatus("1");
						t.setSortmy("1");
						tKpJiageService.updateEntitie(t);

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}


		}
		return j;
	}
	/**
	 * 删除销售价格
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(TKpJiageEntity tKpJiage, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		tKpJiage = systemService.getEntity(TKpJiageEntity.class, tKpJiage.getId());
		message = "销售价格删除成功";
		try{
			tKpJiageService.delete(tKpJiage);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "销售价格删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除销售价格
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格删除成功";
		try{
			for(String id:ids.split(",")){
				TKpJiageEntity tKpJiage = systemService.getEntity(TKpJiageEntity.class, 
				id
				);
				tKpJiageService.delete(tKpJiage);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "销售价格删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加销售价格
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(TKpJiageEntity tKpJiage, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格添加成功";
		try{
			tKpJiage.setBy1(tKpJiage.getCusCode()+tKpJiage.getGdCode()+tKpJiage.getCpCode());
			TKpJiageEntity t = systemService.findUniqueByProperty(TKpJiageEntity.class,"by1",tKpJiage.getBy1());
			if (t != null) {
				MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
				tKpJiage.setSortmy("1");
				tKpJiageService.updateEntitie(t);
			}else{
				TKpCusEntity cusEntity = systemService.findUniqueByProperty(TKpCusEntity.class,"cusCode",tKpJiage.getCusCode());
				if(cusEntity!=null){
					tKpJiage.setCusName(cusEntity.getCusName());
				}
				TKpGongdiEntity kpGongdiEntity = systemService.findUniqueByProperty(TKpGongdiEntity.class,"gdCode",tKpJiage.getGdCode());
				if(kpGongdiEntity!=null){
					tKpJiage.setGdName(kpGongdiEntity.getGdName()) ;
				}
				tKpJiage.setSortmy("1");
				tKpJiageService.save(tKpJiage);
			}
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "销售价格添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

//	/**
//	 * 保存新增/更新的行数据
//	 * @param page
//	 * @return
//	 */
//	@RequestMapping(params = "saveRows")
//	@ResponseBody
//	public AjaxJson saveRows(String orderTyep,sdOrPage page){
//		String message = null;
//		List<TKpJiageEntity> demos=page.getDemos();
//		AjaxJson j = new AjaxJson();
//		if(CollectionUtils.isNotEmpty(demos)){
//			TKpOrderEntity kpOrder = new TKpOrderEntity();
//			List<TKpOrderCpEntity> tKpOrderCpItemList = new ArrayList<TKpOrderCpEntity>() ;
//			for(TKpJiageEntity jeecgDemo:demos){
//				Double amountsum = 0.00;
//				if (StringUtil.isNotEmpty(jeecgDemo.getId())) {
//					try {
//						kpOrder.setCusCode(jeecgDemo.getCusCode());
//						kpOrder.setCusName(jeecgDemo.getCusName());
//						kpOrder.setGdCode(jeecgDemo.getGdCode());
//						kpOrder.setGdName(jeecgDemo.getGdName());
//						kpOrder.setOrStatus("已开票");
//						if(orderTyep.equals("补偿销售")){
//							kpOrder.setOrStatus("开票失败");
//						}
//
//						kpOrder.setOrderType(orderTyep);
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//					try {
//						TKpOrderCpEntity ordercp = new TKpOrderCpEntity();
//						TKpChanpinEntity cpentity = systemService.findUniqueByProperty(TKpChanpinEntity.class,"cpCode",jeecgDemo.getCpCode());
//						ordercp.setCpCode(jeecgDemo.getCpCode());
//						ordercp.setCpName(jeecgDemo.getCpName());
//						ordercp.setCpQty(jeecgDemo.getBy1());
//						ordercp.setCpPrice(jeecgDemo.getCpJiage());
//						ordercp.setCpJifentype(cpentity.getCpJifei());
//						Double ztiji = Double.parseDouble(cpentity.getCpTiji())/1000 * Double.parseDouble(jeecgDemo.getBy1());
//						Double amount = Double.parseDouble(jeecgDemo.getCpJiage()) * Double.parseDouble(jeecgDemo.getBy1());
//						amountsum = amount + amountsum;
//						ordercp.setCpTiji(ztiji.toString());
//						ordercp.setCpAmount(amount.toString());
//						kpOrder.setOrAmount(amountsum.toString());
//						ordercp.setCpName(jeecgDemo.getCpName());
//
//						tKpOrderCpItemList.add(ordercp);
//
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}
//
//			try {
//				String messcheck  = doAddcheck(kpOrder.getCusCode(),kpOrder.getOrAmount());
//				if(kpOrder.getOrderType().equals("正常销售")){
//
//					if (!messcheck.equals("信贷充足")){
//						kpOrder.setOrStatus("开票失败");
//						kpOrder.setOrRemark(messcheck);
//					}
//				}
//
//				tKpOrderService.addMain(kpOrder, tKpOrderCpItemList);
//
//			}catch (Exception e) {
//
//			}
//		}
//		return j;
//	}


	/**
	 * 更新销售价格
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdatefsp")
	@ResponseBody
	public AjaxJson doUpdatefsp(String id, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格反审批成功";
		TKpJiageEntity t = tKpJiageService.get(TKpJiageEntity.class,id);
		try {
			t.setBpmStatus("1");
//			t.setCpJiage(t.getCpDshenjia());
//			t.setCpDshenjia("");

//			MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);

			try {
				TKpJiageLogEntity kplog = new TKpJiageLogEntity();
				MyBeanUtils.copyBeanNotNull2Bean(t,kplog);
				kplog.setYsid(t.getId());
				tKpJiageLogService.save(kplog);
			}catch (Exception e){

			}

			tKpJiageService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "销售价格反审批失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 批量删除销售价格
	 *
	 * @return
	 */
	@RequestMapping(params = "doUpdateshbat")
	@ResponseBody
	public AjaxJson doUpdateshbat(String ids,HttpServletRequest request){
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格批量审核成功";
		try{
			for(String id:ids.split(",")){
				TKpJiageEntity t = systemService.getEntity(TKpJiageEntity.class,
						id
				);
				t.setBpmStatus("3");
				t.setSortmy("9");
				t.setCpJiage(t.getCpDshenjia());
				t.setCpDshenjia("");
				try {
					TKpJiageLogEntity kplog = new TKpJiageLogEntity();
					MyBeanUtils.copyBeanNotNull2Bean(t,kplog);
					kplog.setYsid(t.getId());
					tKpJiageLogService.save(kplog);
				}catch (Exception e){

				}
//			MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
				tKpJiageService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "销售价格批量审核失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 批量删除销售价格
	 *
	 * @return
	 */
	@RequestMapping(params = "doUpdatespbat")
	@ResponseBody
	public AjaxJson doUpdatespbat(String ids,HttpServletRequest request){
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格批量审批成功";
		try{
			for(String id:ids.split(",")){
				TKpJiageEntity t = systemService.getEntity(TKpJiageEntity.class,
						id
				);
				t.setBpmStatus("2");

				try {
					TKpJiageLogEntity kplog = new TKpJiageLogEntity();
					MyBeanUtils.copyBeanNotNull2Bean(t,kplog);
					kplog.setYsid(t.getId());
					tKpJiageLogService.save(kplog);
				}catch (Exception e){

				}
//			MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
				tKpJiageService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "销售价格批量审批失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 更新销售价格
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdatesp")
	@ResponseBody
	public AjaxJson doUpdatesp(String id, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格审批成功";
		TKpJiageEntity t = tKpJiageService.get(TKpJiageEntity.class,id);
		try {
			t.setBpmStatus("2");

			try {
				TKpJiageLogEntity kplog = new TKpJiageLogEntity();
				MyBeanUtils.copyBeanNotNull2Bean(t,kplog);
				kplog.setYsid(t.getId());
				tKpJiageLogService.save(kplog);
			}catch (Exception e){

			}
//			MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
			tKpJiageService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "销售价格审批失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 更新销售价格
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdatesh")
	@ResponseBody
	public AjaxJson doUpdatesh(String id, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格审核成功";
		TKpJiageEntity t = tKpJiageService.get(TKpJiageEntity.class,id);
		try {
			t.setBpmStatus("3");
			t.setSortmy("9");
			t.setCpJiage(t.getCpDshenjia());
			t.setCpDshenjia("");
			try {
				TKpJiageLogEntity kplog = new TKpJiageLogEntity();
				MyBeanUtils.copyBeanNotNull2Bean(t,kplog);
				kplog.setYsid(t.getId());
				tKpJiageLogService.save(kplog);
			}catch (Exception e){

			}
			tKpJiageService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "销售价格审核失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 添加物资出库
	 *
	 * @return
	 */

	@ResponseBody
	public String doAddcheck(String cus_code , String mat_qty ) {
	 String message = "信贷充足";
		try{
			String sql = "SELECT  " +
					"    h.cus_code, " +
					"    (IFNULL((select " +
					"                    ys_jine " +
					"                from " +
					"                    t_kp_yushou " +
					"                where " +
					"                    cus_code = h.cus_code " +
					"                        and ys_type = ' 预收'), " +
					"            0) + IFNULL((select  " +
					"                    sum(or_amount) " +
					"                from " +
					"                    t_kp_order " +
					"                where " +
					"                    cus_code = h.cus_code " +
					"                        and order_type = '补偿销售'), " +
					"            0) - IFNULL(sum(h.or_amount), 0) - IFNULL((select  " +
					"                    cus_xindai " +
					"                from " +
					"                    t_kp_cus " +
					"                where " +
					"                    cus_code = h.cus_code),0)) as xindai " +
					"FROM " +
					"    t_kp_order h " +
					"where " +
					"    h.order_type = '正常销售' " +
					"where cus_code =   '"
					+ cus_code + "'" +
			       "group by h.cus_code ";


			Map<String, Object> binMap	 = systemService.findOneForJdbc(sql);
			Double stock = Double.parseDouble(binMap.get("xindai").toString());
			Double mty = Double.parseDouble( mat_qty );
			if(binMap!=null){
				if( stock < mty){

					message =  cus_code  +"信贷为"+binMap.get("xindai").toString();
				}
			}

//			tWzCkHeadService.addMain(tWzCkHead, tWzCkItemList);
//			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){

		}
		return message;
	}

	/**
	 * 更新销售价格
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(TKpJiageEntity tKpJiage, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "销售价格更新成功";
		TKpJiageEntity t = tKpJiageService.get(TKpJiageEntity.class, tKpJiage.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
			t.setBpmStatus("1");
			t.setSortmy("1");
			tKpJiageService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "销售价格更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 销售价格新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(TKpJiageEntity tKpJiage, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(tKpJiage.getId())) {
			tKpJiage = tKpJiageService.getEntity(TKpJiageEntity.class, tKpJiage.getId());
			req.setAttribute("tKpJiagePage", tKpJiage);
		}
		return new ModelAndView("com/yserp/kpmd/tKpJiage-add");
	}
	/**
	 * 销售价格编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(TKpJiageEntity tKpJiage, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(tKpJiage.getId())) {
			tKpJiage = tKpJiageService.getEntity(TKpJiageEntity.class, tKpJiage.getId());
			req.setAttribute("tKpJiagePage", tKpJiage);
		}
		return new ModelAndView("com/yserp/kpmd/tKpJiage-update");
	}
	
	/**
	 * 导入功能跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name","tKpJiageController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}
	
	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(TKpJiageEntity tKpJiage,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
		CriteriaQuery cq = new CriteriaQuery(TKpJiageEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tKpJiage, request.getParameterMap());
		List<TKpJiageEntity> tKpJiages = this.tKpJiageService.getListByCriteriaQuery(cq,false);
		modelMap.put(NormalExcelConstants.FILE_NAME,"销售价格");
		modelMap.put(NormalExcelConstants.CLASS,TKpJiageEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("销售价格列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
			"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST,tKpJiages);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	 * 导出excel 使模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(TKpJiageEntity tKpJiage,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
    	modelMap.put(NormalExcelConstants.FILE_NAME,"销售价格");
    	modelMap.put(NormalExcelConstants.CLASS,TKpJiageEntity.class);
    	modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("销售价格列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
    	"导出信息"));
    	modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
    	return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<TKpJiageEntity> listTKpJiageEntitys = ExcelImportUtil.importExcel(file.getInputStream(),TKpJiageEntity.class,params);
				for (TKpJiageEntity tKpJiage : listTKpJiageEntitys) {
					tKpJiage.setBpmStatus("1");
					tKpJiage.setBy1(tKpJiage.getCusCode()+tKpJiage.getGdCode()+tKpJiage.getCpCode());
					TKpJiageEntity t = systemService.findUniqueByProperty(TKpJiageEntity.class,"by1",tKpJiage.getBy1());
					if (t != null) {
						MyBeanUtils.copyBeanNotNull2Bean(tKpJiage, t);
						tKpJiageService.updateEntitie(t);
					}else{
						tKpJiageService.save(tKpJiage);
					}
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="销售价格列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<TKpJiageEntity>> list() {
		List<TKpJiageEntity> listTKpJiages=tKpJiageService.getList(TKpJiageEntity.class);
		return Result.success(listTKpJiages);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据ID获取销售价格信息",notes="根据ID获取销售价格信息",httpMethod="GET",produces="application/json")
	public ResponseMessage<?> get(@ApiParam(required=true,name="id",value="ID")@PathVariable("id") String id) {
		TKpJiageEntity task = tKpJiageService.get(TKpJiageEntity.class, id);
		if (task == null) {
			return Result.error("根据ID获取销售价格信息为空");
		}
		return Result.success(task);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="创建销售价格")
	public ResponseMessage<?> create(@ApiParam(name="销售价格对象")@RequestBody TKpJiageEntity tKpJiage, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<TKpJiageEntity>> failures = validator.validate(tKpJiage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		try{
			tKpJiageService.save(tKpJiage);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("销售价格信息保存失败");
		}
		return Result.success(tKpJiage);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="更新销售价格",notes="更新销售价格")
	public ResponseMessage<?> update(@ApiParam(name="销售价格对象")@RequestBody TKpJiageEntity tKpJiage) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<TKpJiageEntity>> failures = validator.validate(tKpJiage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		try{
			tKpJiageService.saveOrUpdate(tKpJiage);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("更新销售价格信息失败");
		}

		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return Result.success("更新销售价格信息成功");
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value="删除销售价格")
	public ResponseMessage<?> delete(@ApiParam(name="id",value="ID",required=true)@PathVariable("id") String id) {
		logger.info("delete[{}]" + id);
		// 验证
		if (StringUtils.isEmpty(id)) {
			return Result.error("ID不能为空");
		}
		try {
			tKpJiageService.deleteEntityById(TKpJiageEntity.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("销售价格删除失败");
		}

		return Result.success();
	}
}
